package com.ruoyi.project.er.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.er.domain.PaeaErError;
import com.ruoyi.project.er.domain.PaeaErPoint;
import com.ruoyi.project.er.model.PaeaErPointModel;
import com.ruoyi.project.er.service.IPaeaErPointService;
import com.ruoyi.project.er.service.IPaeaErTypeService;
import com.ruoyi.project.oms.paeaAuType.domain.PaeaAuType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 风险点定义Controller
 *
 * @author ruoyi
 * @date 2020-03-13
 */
@Controller
@RequestMapping("/er/paeaErPoint")
public class PaeaErPointController extends BaseController
{
    private String prefix = "er/paeaErPoint";

    @Resource
    private IPaeaErPointService paeaErPointService;

    @Resource
    private IPaeaErTypeService paeaErTypeService;

    @RequiresPermissions("er:paeaErPoint:view")
    @GetMapping()
    public String point()
    {
        return prefix + "/paeaErPoint";
    }

    /**
     * 查询风险点定义列表
     */
    @RequiresPermissions("er:paeaErPoint:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaeaErPoint paeaErPoint)
    {
        startPage();
        List<PaeaErPoint> list = paeaErPointService.selectPaeaErPointList(paeaErPoint);
        return getDataTable(list);
    }

    /**
     * 导出风险点定义列表
     */
    @RequiresPermissions("er:paeaErPoint:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PaeaErPoint paeaErPoint)
    {
        List<PaeaErPoint> list = paeaErPointService.selectPaeaErPointList(paeaErPoint);
        ExcelUtil<PaeaErPoint> util = new ExcelUtil<PaeaErPoint>(PaeaErPoint.class);
        return util.exportExcel(list, "point");
    }

    /**
     * 新增风险点定义
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("paeaErTypes", paeaErTypeService.selectPaeaErTypeAll());
        return prefix + "/add";
    }

    /**
     * 新增保存风险点定义
     */
    @RequiresPermissions("er:paeaErPoint:add")
    @Log(title = "风险点定义", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PaeaErPoint paeaErPoint)
    {
        paeaErPoint.setId(paeaErPoint.getCode());
        return toAjax(paeaErPointService.insertPaeaErPoint(paeaErPoint));
    }

    /**
     * 修改风险点定义
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaErPoint paeaErPoint = paeaErPointService.selectPaeaErPointById(id);
        mmap.put("paeaErPoint", paeaErPoint);
        mmap.put("paeaErTypes", paeaErTypeService.selectTypeById(paeaErPoint.getErrTypeId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存风险点定义
     */
    @RequiresPermissions("er:paeaErPoint:edit")
    @Log(title = "风险点定义", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaeaErPoint paeaErPoint)
    {
        return toAjax(paeaErPointService.updatePaeaErPoint(paeaErPoint));
    }

    /**
     * 删除风险点定义
     */
    @RequiresPermissions("er:paeaErPoint:remove")
    @Log(title = "风险点定义", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(paeaErPointService.deletePaeaErPointByIds(ids));
    }
    /**
     * 加载风险点列表树
     * pointTreeData
     */
    @GetMapping("/pointTreeData/{auTypeId}/{errSource}")
    @ResponseBody
    public List<Ztree> pointTreeData(PaeaErPointModel model)
    {
        List<Ztree> ztrees = paeaErPointService.pointTreeData(model);
        return ztrees;

    }
    /**
     * 加载差错风险点
     * selectPaeaErPointsByErrId
     */
    @GetMapping("/selectPaeaErPointsByErrId/{id}")
    @ResponseBody
    public AjaxResult selectPaeaErPointsByErrId(PaeaErError paeaErError)
    {
       // List<Ztree> ztrees = paeaErPointService.pointTreeData(model);

        return AjaxResult.success(paeaErPointService.selectPaeaErPointByErrId(paeaErError.getId()));

    }
}
